/* eslint-disable no-param-reassign */
/* eslint-disable class-methods-use-this */

// 由于服务端渲染的原因 暂时不可以使用 getModule + 动态模块 可由爬 issues 或者查看 nuxt 的方案
// 现在我还没有时间去做调整
import {
  Module,
  VuexModule,
  Action,
  Mutation,
  MutationAction
} from "vuex-module-decorators";

const sleep = (ms: number = 500): Promise<void> =>
  new Promise(res => {
    setTimeout(res, ms);
  });

export type Order = {
  id: number;
  order_sn?: string;
};
export interface OrderState {
  list: Order[];
  total: number;
}

@Module({ name: "order", namespaced: true, stateFactory: true })
export class OrderModule extends VuexModule {
  public list: Order[] = [];
  public total: number = 0;

  @Mutation
  public SET_LIST(payload): void {
    this.list = payload.list;
    this.total = payload.total;
  }

  @MutationAction({ mutate: ["list", "total"], rawError: true })
  public async getList(params?: object): Promise<OrderState> {
    try {
      // @see https://github.com/championswimmer/vuex-module-decorators/issues/118
      console.log("模拟请求中", params);
      await sleep(500);
      return {
        list: [
          { id: 0, order_sn: "111" },
          { id: 1, order_sn: "222" },
          { id: 2, order_sn: "333" }
        ],
        total: 33
      };
    } catch (e) {
      throw e;
      // return e;
    }
  }
}
